# Author: Lovy
# File : common_driver
# Time : 2023-03-17 14:57
from selenium import webdriver
from configs.env import Env



class Singleton:
    """单例"""
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        return cls._instance


class Single:
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Single, cls).__new__(cls, *args, *kwargs)
        return cls._instance


class CommDriver(Singleton):
    """继承单例"""
    driver = None

    def get_driver(self,
                   browser_type=Env.BROWSER_TYPE,
                   headless_flag=Env.HEADLESS_FLAG):
        if self.driver is None:
            if not headless_flag:
                if browser_type == "chrome":
                    self.driver = webdriver.Chrome()
                if browser_type == "edge":
                    self.driver = webdriver.Edge()
            else:
                if browser_type == "chrome":
                    option = webdriver.ChromeOptions()
                    option.add_argument("--headless")
                    self.driver = webdriver.Chrome(options=option)
                if browser_type == "edge":
                    option = webdriver.EdgeOptions()
                    option.add_argument("--headless")
                    self.driver = webdriver.Edge(options=option)
            self.driver.maximize_window()  # 设置窗口最大化
        # self.driver.set_page_load_timeout()     # 设置超时时间
        return self.driver


if __name__ == '__main__':
    test_flag = 3
    if test_flag == 1:
        d1 = CommDriver().get_driver()

    if test_flag == 2:
        d1 = CommDriver()
        d2 = CommDriver()
        d1.get_driver()
        d2.get_driver()

    if test_flag == 3:
        d1 = CommDriver().get_driver()
        d2 = CommDriver().get_driver()
